<template>
  <div class="apply-return">
    <div class="header">
      <div class="red">温馨提示：</div>
      <div class="content">
        <p>1.订单一旦取消，无法恢复</p>
        <p>2.请核对订单信息，如果有问题请及时联系客服</p>
      </div>
    </div>
    <div
      class="heng"
      style="width:100%;height:0.2rem;background:rgba(0, 0, 0, 0.04);"
    ></div>
    <div class="whys">
      <div style="height:0.2rem;"></div>
      <div class="oneDiv" @click="showDiv1()">
        <p class="title">货物状态<span class="dian">*</span></p>

        <span class="why" :class="cartAttr === true ? 'color' : ''">{{
          choiceed1
        }}</span>
        <img src="@assets/images/to-shop.png" alt="" srcset="" />
      </div>
      <div class="oneDiv" @click="showDiv()">
        <p class="title">退款原因<span class="dian">*</span></p>

        <span class="why" :class="cartAttr === true ? 'color' : ''">{{
          choiceed
        }}</span>
        <img src="@assets/images/to-shop.png" alt="" srcset="" />
      </div>
      <div class="oneDiv">
        <!-- <span class="dian"></span> -->
        <span class="title">退款金额：</span>
        <span class="why">{{ pay_price }}</span>
        <!-- <img src="@assets/images/to-shop.png" alt="" srcset=""> -->
      </div>
      <div class="oneDiv">
        <!-- <span class="dian"></span> -->
        <span class="title">退款联系人：</span>
        <span class="why">{{ name }}</span>
        <!-- <img src="@assets/images/to-shop.png" alt="" srcset=""> -->
      </div>
      <div class="oneDiv">
        <!-- <span class="dian"></span> -->
        <span class="title">联系方式：</span>
        <span class="why">{{
          phone.substr(0, 3) + "****" + phone.substr(7)
        }}</span>
        <!-- <img src="@assets/images/to-shop.png" alt="" srcset=""> -->
      </div>
    </div>
    <div
      class="heng"
      style="width:100%;height:0.2rem;background:rgba(0, 0, 0, 0.04);"
    ></div>
    <div class="textareaDiv">
      <div class="title1">
        <!-- <span class="dian"></span> -->
        <span class="title">补充描述和凭证</span>
      </div>
      <textarea
        placeholder="填写备注信息，200字以内"
        class="num"
        maxlength="200"
        v-model="refund_reason_wap_explain"
      ></textarea>
      <p class="wordNum">{{ refund_reason_wap_explain.length }}/200</p>
    </div>
    <div class="uploadImg textareaDiv" style="border:none;">
      <div class="title1">
        <!-- <span class="dian"></span> -->
        <span class="title">上传凭证，最多6张</span>
      </div>
      <div class="upload acea-row row-middle">
        <div
          class="pictrue"
          v-for="(img, index) in refund_reason_wap_img"
          :key="img"
        >
          <img :src="img" />
          <div
            class="iconfont icon-guanbi1 font-color-red"
            @click="refund_reason_wap_img.splice(index, 1)"
          ></div>
        </div>
        <VueCoreImageUpload
          class="btn btn-primary"
          :crop="false"
          compress="80"
          @imageuploaded="imageuploaded"
          :headers="headers"
          :max-file-size="5242880"
          :credentials="false"
          inputAccept="image/*"
          inputOfFile="file"
          :url="url"
          v-if="refund_reason_wap_img.length < 9 && !isAnzhuowebview"
        >
          <div class="pictrue acea-row row-center-wrapper row-column">
            <img
              style="width:2rem;height:2rem;"
              src="@assets/images/uploadImg.png"
              alt=""
              srcset=""
            />
          </div>
        </VueCoreImageUpload>
        <div class="" v-if="isAnzhuowebview">
          <div
            class="pictrue acea-row row-center-wrapper row-column"
            @click="change"
          >
            <img
              style="width:2rem;height:2rem;"
              src="@assets/images/uploadImg.png"
              alt=""
              srcset=""
            />
          </div>
        </div>
      </div>
    </div>
    <div>
      <div
        class="product-window group-con"
        :class="cartAttr === true ? 'on' : ''"
        :style="'padding-bottom:' + (isShowBtn ? '0' : '')"
      >
        <div class="title">申请原因</div>
        <div class="whyBack">
          <div
            class="oneReson"
            v-for="oneResone in resone"
            :key="oneResone.id"
            @click="changeType(oneResone)"
          >
            <span>{{ oneResone.content }}</span>
            <img
              v-if="oneResone.type == '1'"
              src="@assets/images/checked.png"
              alt=""
            />
            <img
              v-if="oneResone.type == '0'"
              src="@assets/images/choice1.png"
              alt=""
            />
          </div>
        </div>
        <div class="sure" @click="closeAttr">取消</div>
      </div>
      <div
        class="mask"
        @touchmove.prevent
        :hidden="cartAttr === false"
        @click="closeAttr"
      ></div>
    </div>
    <div>
      <div
        class="product-window group-con"
        :class="cartAttr1 === true ? 'on' : ''"
        :style="'padding-bottom:' + (isShowBtn ? '0' : '')"
      >
        <div class="title">申请原因</div>
        <div class="whyBack">
          <div
            class="oneReson"
            v-for="oneResone in resone1"
            :key="oneResone.id"
            @click="changeType1(oneResone)"
          >
            <span>{{ oneResone.content }}</span>
            <img
              v-if="oneResone.type == '1'"
              src="@assets/images/checked.png"
              alt=""
            />
            <img
              v-if="oneResone.type == '0'"
              src="@assets/images/choice1.png"
              alt=""
            />
          </div>
        </div>
        <div class="sure" @click="closeAttr1">取消</div>
      </div>
      <div
        class="mask"
        @touchmove.prevent
        :hidden="cartAttr1 === false"
        @click="closeAttr1"
      ></div>
    </div>
    <!-- <div
      class="goodsStyle acea-row row-between"
      v-for="cart in orderInfo.cartInfo"
      :key="cart.id"
    >
      <div class="pictrue">
        <img :src="cart.productInfo.image" class="image" />
      </div>
      <div class="text acea-row row-between">
        <div class="name line2">{{ cart.productInfo.store_name }}</div>
        <div class="money">
          <div>
            ￥{{
              cart.productInfo.attrInfo
                ? cart.productInfo.attrInfo.price
                : cart.productInfo.price
            }}
          </div>
          <div class="num">x{{ cart.cart_num }}</div>
        </div>
      </div>
    </div> -->
    <!-- <div class="list">
      <div class="item acea-row row-between-wrapper">
        <div>退货件数</div>
        <div class="num">{{ orderInfo.total_num }}</div>
      </div>
      <div class="item acea-row row-between-wrapper">
        <div>退款金额</div>
        <div class="num">￥{{ orderInfo.pay_price }}</div>
      </div>
      
      <div class="item textarea acea-row row-between">
        <div>备注说明</div>
        <textarea
          placeholder="填写备注信息，100字以内"
          class="num"
          v-model="refund_reason_wap_explain"
        ></textarea>
      </div>
      <div class="item acea-row row-between">
        <div class="title acea-row row-between-wrapper">
          <div>上传凭证</div>
          <div class="tip">( 最多可上传3张 )</div>
        </div>
        <div class="upload acea-row row-middle">
          <div
            class="pictrue"
            v-for="(img, index) in refund_reason_wap_img"
            :key="img"
          >
            <img :src="img" />
            <div
              class="iconfont icon-guanbi1 font-color-red"
              @click="refund_reason_wap_img.splice(index, 1)"
            ></div>
          </div>
          <VueCoreImageUpload
            class="btn btn-primary"
            :crop="false"
            compress="80"
            @imageuploaded="imageuploaded"
            :headers="headers"
            :max-file-size="5242880"
            :credentials="false"
            inputAccept="image/*"
            inputOfFile="file"
            :url="url"
            v-if="refund_reason_wap_img.length < 3"
          >
            <div class="pictrue acea-row row-center-wrapper row-column">
              <span class="iconfont icon-icon25201"></span>
              <div>上传凭证</div>
            </div>
          </VueCoreImageUpload>
        </div>
      </div>
    </div> -->
    <div class="returnBnt bg-color-red" @click="submit">申请退款</div>
  </div>
</template>

<script>
import { orderDetail, getRefundReason, postOrderRefund } from "@api/order";
import { trim, VUE_APP_API_URL } from "@utils";
import cookie from "@utils/store/cookie";
import VueCoreImageUpload from "vue-core-image-upload";
import { connect } from "tls";

export default {
  name: "goodsReturn",
  components: {
    VueCoreImageUpload
  },
  data() {
    return {
      url: `${VUE_APP_API_URL}/upload/image`,
      headers: {
        "Authori-zation": "Bearer " + this.$store.state.app.token
      },
      id: this.$route.params.id || 0,
      orderInfo: {},
      reasonList: [],
      reason: "",
      refund_reason_wap_explain: "",
      refund_reason_wap_img: [],
      cartAttr: false,
      resone: [
        {
          type: "0",
          content: "发错货"
        },
        {
          type: "0",
          content: "商品损坏"
        },
        {
          type: "0",
          content: "不想要了"
        },
        {
          type: "0",
          content: "商品与页面描述不符"
        }
      ],
      cartAttr1: false,
      resone1: [
        {
          type: "0",
          content: "未收到货"
        },
        {
          type: "0",
          content: "已收到货"
        }
      ],
      choiceed1: "请选择",
      choiceed: "请选择",
      name: "",
      phone: "",
      pay_price: "",
      isAnzhuowebview: "",
      is_diff: ""
    };
  },
  methods: {
    getElementOffsetTop(el) {
      let top = el.offsetTop;
      let cur = el.offsetParent;
      while (cur != null) {
        top += cur.offsetTop;
        cur = cur.offsetParent;
      }
      return top;
    },

    componentDidMount() {
      const u = navigator.userAgent;
      const isAndroid = u.indexOf("Android") > -1 || u.indexOf("Linux") > -1; //android终端
      // alert('android'+isAndroid)
      if (isAndroid) {
        // android统一处理，不影响ios的自身处理
        const body = document.getElementsByTagName("body")[0]; // 兼容获取body
        const regDom = document.querySelector(".order-submission"); // 获取页面根元素
        const content = document.querySelector(".order-submission"); // 表单内容部分

        // const scrollHeight = body.scrollHeight // 网页文档高
        // const scrollTop = body.scrollTop// 卷上去的高

        const clientHeight = body.clientHeight; //可见高
        const fixHeight = clientHeight / 3; // 定位高，弹出键盘时input距浏览器上部的距离，自己定义合适的

        // 符合需弹出键盘的元素query
        const queryStr =
          'input[type="text"], input[type="tel"], input[type="password"], textarea';
        const inputs = content.querySelectorAll(queryStr);

        // console.log(inputs)

        const offsetTopArr = Array.prototype.map.call(inputs, item => {
          return this.getElementOffsetTop(item); // offsetTop只能获取到顶部距它的offsetParent的距离，需此方法获取到元素距顶部的距离
        });

        inputs.forEach((item, i) => {
          item.addEventListener("focus", () => {
            // 改变top上移页面
            regDom.style.top = "-" + (offsetTopArr[i] - fixHeight) + "px";
          });

          item.addEventListener("blur", () => {
            // 恢复top
            regDom.style.top = 0;
          });
        });
      }
    },
    onWebResponse: function(str) {
      var obj = JSON.parse(str);
      var action = obj.action;
      var self = this;
      var content = obj.content;
      if (action == "onUploadImageSuccess") {
        this.refund_reason_wap_img.push(content.imageUrl);
      }
    },
    closeAttr: function() {
      this.cartAttr = false;
    },
    closeAttr1: function() {
      this.cartAttr1 = false;
    },
    showDiv: function() {
      this.cartAttr = true;
    },
    showDiv1: function() {
      this.cartAttr1 = true;
    },
    changeType: function(data) {
      for (var i = 0; i < this.resone.length; i++) {
        if (data.content == this.resone[i].content) {
          this.resone[i].type = "1";
          this.choiceed = data.content;
        } else {
          this.resone[i].type = "0";
        }
      }
      this.cartAttr = false;
    },
    changeType1: function(data) {
      for (var i = 0; i < this.resone1.length; i++) {
        if (data.content == this.resone1[i].content) {
          this.resone1[i].type = "1";
          if(this.choiceed11 != data.content){
            this.choiceed = '请选择';
            if(data.content == '未收到货'){
              this.resone=[
                {
                  type: "0",
                  content: "不喜欢/不想要了"
                },
                {
                  type: "0",
                  content: "空包裹"
                },
                {
                  type: "0",
                  content: "快递/物流一直未收到"
                },
                {
                  type: "0",
                  content: "快递物流无跟踪记录"
                },
                {
                  type: "0",
                  content: "货物破损已拒签"
                }
              ]
            }
            if(data.content == '已收到货'){
              this.resone=[
                {
                  type: "0",
                  content: "七天无理由退货"
                },
                {
                  type: "0",
                  content: "质量问题"
                },
                {
                  type: "0",
                  content: "商品与描述不符"
                },
                {
                  type: "0",
                  content: "货物破损已拒签"
                },
                {
                  type: "0",
                  content: "其它"
                }
              ]
            }
          }
          this.choiceed1 = data.content;
        } else {
          this.resone1[i].type = "0";
        }
      }
      
      
      this.cartAttr1 = false;
    },
    change: function() {
      if (this.refund_reason_wap_img.length == 6) {
        return this.$dialog.error("最多上传6张");
      }
      var u = navigator.userAgent,
        app = navigator.appVersion;
      var isAndroid = u.indexOf("Android") > -1 || u.indexOf("Linux") > -1; //g
      var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
      var obj1 = {
        action: "uploadImage",
        content: {
          actType: "feedback"
        }
      };
      if (isAndroid) {
        App.onAppResponse(JSON.stringify(obj1));
      }
    },
    imageuploaded(res) {
      if (res.status !== 200)
        return this.$dialog.error(res.msg || "上传图片失败");
      this.refund_reason_wap_img.push(res.data.url);
    },
    getOrderDetail() {
      orderDetail(this.id)
        .then(res => {
          this.orderInfo = res.data;
        })
        .catch(err => {
          this.$dialog.error(err.msg || "获取订单失败");
        });
    },
    getRefundReason() {
      getRefundReason().then(res => {
        this.reasonList = res.data;
      });
    },
    submit() {
      const refund_reason_wap_explain = trim(this.refund_reason_wap_explain),
        text = this.choiceed,
        apply_status = this.choiceed1;
      // if(this.is_diff == '1'){
      if (apply_status == "请选择")
        return this.$dialog.toast({ mes: "请选择货物状态" });
      if (text == "请选择")
        return this.$dialog.toast({ mes: "请选择退款原因" });
      // if (this.refund_reason_wap_img.length < 1)
      //   return this.$dialog.toast({ mes: "请上传图片" });
      // }
      postOrderRefund({
        text,
        apply_status,
        uni: this.orderInfo.order_id,
        refund_reason_wap_img: this.refund_reason_wap_img.join(","),
        refund_reason_wap_explain
      })
        .then(res => {
          this.$dialog.success("提交成功，请留意您的订单状态");
          this.$router.go(-1);
        })
        .catch(res => {
          this.$dialog.error(res.msg);
        });
    }
  },
  mounted() {
    if (cookie.get("from1") == "app") {
      this.name = this.$route.query.name;
    } else {
      this.name = this.$route.query.name;
    }
    this.phone = this.$route.query.phone;
    this.pay_price = this.$route.query.pay_price;
    this.is_diff = this.$route.query.is_diff;

    var that = this;
    // alert(this.$route.query.name)
    // console.log(cookie.get('from1'));
    if (cookie.get("from1") == "app") {
      window["onWebResponse"] = str => {
        that.onWebResponse(str);
      };
      var u = navigator.userAgent,
        app = navigator.appVersion;
      var isAndroid = u.indexOf("Android") > -1 || u.indexOf("Linux") > -1; //g
      var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
      if (isAndroid) {
        this.isAnzhuowebview = true;
      } else {
        this.isAnzhuowebview = false;
      }
    } else {
      this.isAnzhuowebview = false;
    }

    // alert(this.phone);

    this.getOrderDetail();
    this.getRefundReason();
    // if(this.is_diff == '1'){
    //   this.componentDidMount();
    // }
  }
};
</script>
<style scoped>
.upload .pictrue {
  position: relative;
}
.apply-return {
  background: #fff;
}
.pictrue .iconfont {
  position: absolute;
  right: 0;
  top: 0;
}
.upload .pictrue img {
  float: left;
  width: 2rem;
  height: 2rem;
  margin-top: 0.2rem;
  border-radius: 0.16rem;
}
.product-window {
  position: fixed;
  bottom: 0;
  width: 100%;
  left: 0;
  background-color: #fff;
  z-index: 778;
  border-radius: 0.36rem 0.36rem 0 0;
  transform: translate3d(0, 100%, 0);
  -webkit-transform: translate3d(0, 100%, 0);
  -ms-transform: translate3d(0, 100%, 0);
  -moz-transform: translate3d(0, 100%, 0);
  -o-transform: translate3d(0, 100%, 0);
  transition: all 0.3s cubic-bezier(0.25, 0.5, 0.5, 0.9);
  -webkit-transition: all 0.3s cubic-bezier(0.25, 0.5, 0.5, 0.9);
  -moz-transition: all 0.3s cubic-bezier(0.25, 0.5, 0.5, 0.9);
  -o-transition: all 0.3s cubic-bezier(0.25, 0.5, 0.5, 0.9);
}
.product-window.on {
  transform: translate3d(0, 0, 0);
  -webkit-transform: translate3d(0, 0, 0);
  -ms-transform: translate3d(0, 0, 0);
  -moz-transform: translate3d(0, 0, 0);
  -o-transform: translate3d(0, 0, 0);
}
.product-window .title {
  height: 1.08rem;
  width: 100%;
  text-align: center;
  line-height: 1.08rem;
  font-size: 0.3rem;
  color: rgba(0, 0, 0, 0.7);
  font-weight: bold;
}
.product-window .whyBack {
  width: 100%;
  height: auto;
  overflow: hidden;
  padding-bottom: 0.16rem;
  background: rgba(30, 28, 42, 0.05);
}
.product-window .whyBack .oneReson {
  background: #fff;
  width: 100%;
  height: 1.12rem;
  border-top: rgba(0, 0, 0, 0.1) solid 0.02rem;
  box-sizing: border-box;
  overflow: hidden;
}
.product-window .whyBack .oneReson span {
  padding-left: 0.3rem;
  color: rgba(0, 0, 0, 0.7);
  float: left;
  line-height: 1;
  padding-top: 0.41rem;
  font-size: 0.3rem;
}
.product-window .whyBack .oneReson img {
  width: 0.33rem;
  height: 0.33rem;
  float: right;
  margin: 0.34rem 0.3rem;
}
.product-window .sure {
  height: 1.12rem;
  width: 100%;
  text-align: center;
  line-height: 1.12rem;
  font-size: 0.3rem;
  color: rgba(0, 122, 255, 1);
  /* font-weight: bold; */
}
.header {
  margin: 0.46rem 0.3rem 0 0.3rem;
  width: 6.9rem;
}
.header .red {
  color: rgba(255, 60, 57, 1);
  font-size: 0.24rem;
  line-height: 0.34rem;
  padding-bottom: 0.22rem;
}
.header .content {
  width: 100%;
  height: auto;
  padding-bottom: 0.22rem;
  /* border-bottom: rgba(0, 0, 0, 0.1) 0.02rem solid; */
}
.header .content p {
  font-size: 0.24rem;
  color: rgba(0, 0, 0, 0.6);
  line-height: 0.34rem;
  padding-bottom: 0.1rem;
  margin-bottom: 0.1rem;
}
.whys {
  padding: 0.4rem 0.3rem 0 0.3rem;
  /* width: 6.9rem; */
  height: auto;
  overflow: hidden;
  /* border-bottom: rgba(0, 0, 0, 0.1) 0.02rem solid; */
}
.whys .oneDiv {
  width: 100%;
  margin-bottom: 0.22rem;
  height: 0.5rem;
  overflow: hidden;
}
.whys .oneDiv .dian {
  /* float: left; */
  /* margin-top: 0.13rem; */
  /* margin-right: 0.12rem; */
  color: rgba(255, 60, 57, 1);
}
.whys .oneDiv .title {
  float: left;
  line-height: 1;
  padding: 0.05rem 0;
  font-size: 0.28rem;
  width: 1.8rem;
  color: rgba(0, 0, 0, 1);
}
.whys .oneDiv .why {
  float: left;
  line-height: 1;
  padding: 0.05rem 0;
  font-size: 0.28rem;
  padding-right: 0.34rem;
  color: rgba(0, 0, 0, 1);
}
.whys .oneDiv img {
  float: right;
  width: 0.3rem;
  margin-top: 0.02rem;
}
.textareaDiv {
  margin: 0.22rem 0.3rem 0.12rem 0.3rem;
  width: 6.9rem;
  height: auto;
  overflow: hidden;
  /* border-bottom: rgba(0, 0, 0, 0.1) 0.02rem solid; */
  position: relative;
}
.textareaDiv .title1 {
  width: 100%;
  margin-bottom: 0.22rem;
  height: 0.34rem;
  overflow: hidden;
}
.textareaDiv .wordNum {
  position: absolute;
  bottom: 0.2rem;
  right: 0.2rem;
  font-size: 0.28rem;
  color: rgba(0, 0, 0, 0.4);
}
.textareaDiv .title1 .dian {
  float: left;
  margin-top: 0.13rem;
  margin-right: 0.12rem;
  width: 0.08rem;
  height: 0.08rem;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.3);
}
.textareaDiv .title1 .title {
  float: left;
  line-height: 1;
  padding: 0.05rem 0;
  font-size: 0.28rem;
  padding-right: 0.34rem;
  color: rgba(0, 0, 0, 0.4);
}
.textareaDiv textarea {
  padding: 0.2rem 0.2rem;
  width: 100%;
  height: 2rem;
  background: #f5f5f5;
  font-size: 0.24rem;
  line-height: 0.34rem;
}
.line2 {
  word-break: break-all;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
</style>
